<template>
    <div>
        <el-form :model="form" label-width="100px">
            <el-form-item label="任务名">
                <el-input v-model="form.jobDetails.jobName" style="width: 250px;"></el-input>
            </el-form-item>
            <el-form-item label="任务组">
                <el-input v-model="form.jobDetails.jobGroup" style="width: 250px;"></el-input>
            </el-form-item>
            <el-form-item label="任务执行类">
                <el-input v-model="form.jobDetails.jobClassName" style="width: 250px;"></el-input>
            </el-form-item>

            <el-form-item label="触发器名">
                <el-input v-model="form.cronTriggers.triggerName" style="width: 250px;"></el-input>
            </el-form-item>
            <el-form-item label="触发器组">
                <el-input v-model="form.cronTriggers.triggerGroup" style="width: 250px;"></el-input>
            </el-form-item>
            <el-form-item label="cron表达式">
                <el-input v-model="form.cronTriggers.cronExpression" style="width: 250px;"></el-input>
            </el-form-item>
            <el-form-item label="任务描述">
                <el-input type="textarea" v-model="form.jobDetails.description" style="width: 250px;"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="handleSubmit">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {JobDetailsService} from "../../../model/gs/JobDetailsService";

    const jobDetailsService = JobDetailsService.getInstance()

    export default {
        data() {
            return {
                operation: 'save',
                form: {
                    jobDetails: {
                        jobName: '',
                        jobGroup: '',
                        jobClassName: '',
                        description: ''
                    },
                    cronTriggers: {
                        triggerName: '',
                        triggerGroup: '',
                        cronExpression: ''
                    }
                }
            }
        },
        methods: {
            init(jobName) {
                //判断是否传了任务名
                if (jobName) {
                    this.operation = 'update'
                    jobDetailsService.info(jobName).then(({data}) => {
                        if(data.code === 0){
                            this.form = data.data
                        }
                    })
                } else {
                    this.operation = 'save'
                }
            },
            handleSubmit() {
                //判断是新增还是修改
                if (this.operation === 'save') {
                    jobDetailsService.save(this.form).then(({data}) => {
                        if(data.code === 0){
                            this.$message.success({
                                message: '添加任务成功',
                                duration: 1000
                            })
                            this.$emit('completed')
                        }
                    })
                } else {
                    jobDetailsService.update(this.form).then(({data}) => {
                        if(data.code === 0){
                            this.$message.success({
                                message: '任务修改成功',
                                duration: 1000
                            })
                            this.$emit('completed')
                        }
                    })
                }
            }
        }
    }
</script>

<style scoped>

</style>
